  /**
   * 数据库基本操作(增删改查)
   */
module.exports = function() {
    //数据库对象模型
    let mongodbModel = {};
    let _dbName, _collections, _MongoClient, _url;
  
  /**
   * 初始化
   * init(url, dbName, collections) 
   *    url   要连接数据库的ip地址和端口 (mongodb://localhost:27017)
   *    dbName  连接到数据库的那一个库
   *    collections 连接库中的集合
   */
    mongodbModel.init = function(url, dbName, collections) {
      _MongoClient = require("mongodb").MongoClient;
      _url = url;
      _dbName = dbName;
      _collections = collections;
    };
  
    /**
     * add(condition)
     * condition: 必须是一个JSON格式的object对象 
     * 如:{ "nickname":'陈灿明', 'password':'272244064', age:'18', interest:[游戏, 电影, 小说, 漫画] }
     *
     *    return result
     *    error: return error info
     *    success: {result: { ok: 1, n: 1 }
     * }
     */
    mongodbModel.add = function(condition) {
      return new Promise((resolve, reject) => {
        _MongoClient.connect(
          _url,
          (err, client) => {
            if (err) {
              throw new Error(err);
            }
            const db = client.db(_dbName);
            const collection = db.collection(_collections);
            collection.insert(condition, function(err, result) {
              err ? reject(err) : resolve(result.result);
              client.close();
            });
          }
        );
      });
    };
  
    /**
     * 查找昵称
     * findUser(condition)
     *    condition 必须是一个JSON格式的object对象 如:{"nickname":'陈灿明'}
     *
     *    return  result
     *    error: return error info
     *    success: return target object set(this object is array type) if object for null return object is length
     */
    mongodbModel.find = function(condition) {
      return new Promise((resolve, reject) => {
        _MongoClient.connect(
          _url,
          (err, client) => {
            if (err) {
              throw new Error(err);
            }
            const db = client.db(_dbName);
            const collection = db.collection(_collections);
            collection.find(condition).toArray((err, result) => {
              err
                ? reject(err)
                : result.length
                  ? resolve(result)
                  : resolve(result.length);
              client.close();
            });
          }
        );
      });
    };
  
    /**
     * 删除用户
     * deleteUser(condition)
     *    condition 必须是一个JSON格式的object对象 如:{"nickname":'陈灿明'}
     *
     *    return  result
     *    error: return error info
     *    { n: 0, ok: 1 } 查找成功但没有用户
     *    { n: 1, ok: 1 } 查找成功并删除用户
     */
    mongodbModel.delete = function(condition) {
      return new Promise((resolve, reject) => {
        _MongoClient.connect(
          _url,
          (err, client) => {
            if (err) {
              throw new Error(err);
            }
            const db = client.db(_dbName);
            const collection = db.collection(_collections);
            collection.deleteOne(condition, (err, result) => {
              err ? reject(err) : resolve(result.result);
              client.close();
            });
          }
        );
      });
    };
  
    
    /**
     *    更新
     *    update(condition, data) 
     *    condition 必须是一个JSON格式的object对象 如:{"nickname":'陈灿明'}
     *    data      必须是一个JSON格式的object对象 如:{"age":'18'}
     *
     *    return  result
     *    查找错误: 返回错误信息
     *    { n: 0, nModified: 0, ok: 1 } 查找成功 但没有用户
     *    { n: 1, nModified: 1, ok: 1 } 查找成功 并更新数据
     */
    mongodbModel.update = function(condition, data) {
      return new Promise((resolve, reject) => {
        _MongoClient.connect(
          _url,
          (err, client) => {
            const db = client.db(_dbName);
            const collection = db.collection(_collections);
            collection.updateOne(condition, data, (err, result) => {
              err ? reject(err) : resolve(result.result);
              client.close();
            });
          }
        );
      });
    };
  
    return mongodbModel;
  };
  